package com.threeti.mecool.core.infrastructure.persistence;

import com.threeti.mecool.core.domain.model.project.Project;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import java.util.Collection;

public abstract class ProjectCriteria {

    public static Specification<Project> likeProjectName(final String projName) {
        return new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Path<String> projNamePath = root.get("name");
                return builder.like(projNamePath, "%" + projName + "%");
            }
        };
    }

    public static Specification<Project> eqProjectCategory(final Long projManagerId) {
        return new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Path<Long> projManagerIdPath = root.get("category").get("id");
                return builder.equal(projManagerIdPath, projManagerId);
            }
        };
    }

    public static Specification<Project> eqProjectManager(final Long projManagerEmpId) {
        return new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Path<Long> projManagerEmpIdPath = root.get("managerEmpId");
                return builder.equal(projManagerEmpIdPath, projManagerEmpId);
            }
        };
    }

    public static Specification<Project> eqUserId(final Long userId) {
        return new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Path<Long> userIdPath = root.get("user").get("id");
                return builder.equal(userIdPath, userId);
            }
        };
    }

    public static Specification<Project> idIn(final Collection<Long> idList) {
        return new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return root.get("id").in(idList);
            }
        };
    }

    //TODO jay:support date range search condition...
//  public static Specification<Project> betweenStartAndEndDate(final Date startDate, final Date endDate) {
//    return new Specification<Project>() {
//      @Override
//      public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
//        Path<Date> projStartDatePath = root.get("startDate");
//        Path<Date> projEndDatePath = root.get("endDate");
//        return builder.b
//      }
//    };
//  }
}
